/**
 * 用户信息页面
 * author wim
 */
import 'dart:convert';
import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';

import '../base/Constant.dart';
import '../util/BlankToolBarTool.dart';

class UserInfoPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return UserInfoView();
  }
}

class UserInfoView extends State<UserInfoPage> {
  int groupValue = 1;
  String token = "";
  String nickName = "";
  String signature = "";
  String province = "";
  String city = "";
  String county = "";
  String birthday = "";
  String sex = "1";
  TextEditingController nameControl = TextEditingController();
  BlankToolBarModel barModel = BlankToolBarModel();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    barModel.outSideCallback = focusNodeChange;
  }

  void focusNodeChange() {
    setState(() {});
  }

  @override
  void dispose() {
    barModel.removeFocusListeners();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: new AppBar(
        title: new Text(
          "个人资料",
          style: TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
              color: Colors.black),
        ),
        elevation: 0,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
      ),
      body: BlankToolBarTool.blankToolBarWidget(context,
          model: barModel,
          body: new Container(
            color: divideColor,
            child: new Column(
              children: <Widget>[
                Container(
                  color: Colors.white,
                  width: double.infinity,
                  padding:
                      EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_50)),
                  child: Column(
                    children: <Widget>[
                      GestureDetector(
                        child: ClipOval(
                          child: CachedNetworkImage(
                imageUrl:
                            "https://upload.jianshu.io/users/upload_avatars/4348529/8b04d4ebc819?imageMogr2/auto-orient/strip|imageView2/1/w/96/h/96/format/webp",
                            width: ScreenUtil.getInstance().setHeight(DP_150),
                            height: ScreenUtil.getInstance().setHeight(DP_150),
                            fit: BoxFit.cover,
                          ),
                        ),
                        onTap: () {},
                      ),
                      Container(
                        height: DP_10,
                      ),
                      Text('点击头像修改',style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),color: Colors.black87),)
                    ],
                  ),
                ),
                Container(height: ScreenUtil.getInstance().setHeight(DP_20)),
                new Container(
                  padding: EdgeInsets.fromLTRB(15.0, 5.0, 15.0, 5.0),
                  color: Colors.white,
                  child: new Row(
                    children: <Widget>[
                      Expanded(
                        flex: 1,
                        child: Text(
                          '昵称',
                          style: TextStyle(fontSize: 15.0),
                        ),
                      ),
                      new Expanded(
                          flex: 3,
                          child: new TextField(
                            onChanged: (value) {
                              nickName = value;
                            },
                            cursorColor: mainColor,
                            keyboardAppearance: Brightness.light,
                            maxLines: 1,
                            controller: TextEditingController.fromValue(
                                TextEditingValue(
                                    text: nickName,
                                    selection: TextSelection.fromPosition(
                                        TextPosition(
                                            affinity: TextAffinity.downstream,
                                            offset: nickName.length)))),
                            textAlign: TextAlign.right,
                            decoration: new InputDecoration(
                                hintText: "请输入昵称",
                                border: InputBorder.none,
                                hintStyle: TextStyle(fontSize: 15.0)),
                          )),
                    ],
                  ),
                ),
                new Container(
                  padding: EdgeInsets.fromLTRB(15.0, 0.0, 15.0, 0.0),
                  color: Colors.white,
                  margin: EdgeInsets.fromLTRB(0.0, 1.0, 0.0, 0.0),
                  child: new Row(
                    children: <Widget>[
                      Expanded(
                        flex: 1,
                        child: Text(
                          '性别',
                          style: TextStyle(fontSize: 15.0),
                        ),
                      ),
                      new Container(
                        alignment: Alignment.centerRight,
                        child: new Row(
                          children: <Widget>[
                            new Radio(
                                value: 1,
                                activeColor: mainColor,
                                groupValue: groupValue,
                                onChanged: (T) {
                                  setState(() {
                                    groupValue = T;
                                  });
                                  // updateGroupValue(T);
                                }),
                            Text('男'),
                            new Radio(
                                value: 2,
                                groupValue: groupValue,
                                activeColor: mainColor,
                                onChanged: (T) {
                                  setState(() {
                                    groupValue = T;
                                  });
                                }),
                            Text('女'),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.fromLTRB(0.0, 1.0, 0.0, 0.0),
                  padding: EdgeInsets.fromLTRB(15.0, 15.0, 15.0, 15.0),
                  color: Colors.white,
                  child: new GestureDetector(
                    onTap: () {
                      showMonth(context);
                    },
                    child: new Row(
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: Text(
                            '生日',
                            style: TextStyle(fontSize: 15.0),
                          ),
                        ),
                        Container(
                          child: Text(
                            birthday,
                            style: TextStyle(fontSize: 16, color: Colors.black),
                          ),
                        ),
                        Icon(
                          Icons.arrow_forward_ios,
                          color: Colors.black87,
                          size: 20,
                        )
                      ],
                    ),
                  ),
                ),
                new Container(
                  width: ScreenUtil.getInstance().setWidth(550.00),
                  height: 45,
                  margin: EdgeInsets.fromLTRB(0, 30, 0, 0),
                  decoration: new BoxDecoration(
                       color:mainColor,
                      borderRadius: BorderRadius.circular(20.0)),
                  padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                  child: new FlatButton(
                    child: new Padding(
                      padding: EdgeInsets.all(0.0),
                      child: new Text(
                        "保存信息",
                        style: new TextStyle(color: Colors.white, fontSize: 16),
                      ),
                    ),
                    onPressed: () {
                      if (nickName == "") {
                        Fluttertoast.showToast(msg: "昵称必须填写");
                        return;
                      }
                      saveInfo();
                    },
                  ),
                )
              ],
            ),
          )),
    );
  }

  saveInfo() async {
    //
    FormData formData = new FormData.fromMap({
      "token": token,
      "birthday": birthday,
      "province": province,
      "city": city,
      "county": county,
      "sex": groupValue.toString(),
      "signature": signature,
      "nickname": nickName
    });
    Response<String> response =
        await HttpUtil().post(Api.EDIT_USERINFO, data: formData);
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];

    Fluttertoast.showToast(msg: msg);
    // print(userMap['data'].toString());
    if (code == 0) {
      Navigator.of(context).pop();
    }
  }

  void showMonth(BuildContext context) {
    // DatePicker.showDatePicker(context,
    //     showTitleActions: true, onChanged: (date) {}, onConfirm: (date) {
    //   setState(() {
    //     birthday = DateUtils.instance.getFormartData(
    //         timeSamp: date.millisecondsSinceEpoch, format: "yyyy-MM-dd");
    //   });
    // }, currentTime: DateTime.now(), locale: LocaleType.zh);
  }
}
